#!/bin/bash

# 获取当前系统信息
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)

if [[ $1 =~ "linux" ]]; then
    OS="linux"
    ARCH="x86_64"
fi

# 设置输出目录和可执行文件名
OUTPUT_DIR="output"
EXECUTABLE="app"  # 替换为您的应用名称
HTML_DIR="templates"

# 根据系统架构设置GOARCH
case "$ARCH" in
    x86_64)
        GOARCH="amd64"
        ;;
    i686|i386)
        GOARCH="386"
        ;;
    aarch64|arm64)
        GOARCH="arm64"
        ;;
    arm*)
        GOARCH="arm"
        ;;
    *)
        echo "Unsupported architecture: $ARCH"
        exit 1
        ;;
esac

# 根据操作系统设置GOOS
case "$OS" in
    linux)
        GOOS="linux"
        ;;
    darwin)
        GOOS="darwin"
        ;;
    windows)
        GOOS="windows"
        EXECUTABLE="$EXECUTABLE.exe"
        ;;
    *)
        echo "Unsupported OS: $OS"
        exit 1
        ;;
esac

# 创建输出目录
mkdir -p "$OUTPUT_DIR"

# 编译项目
echo "Building for $GOOS/$GOARCH..."
env GOOS=$GOOS GOARCH=$GOARCH go build -o "$OUTPUT_DIR/$EXECUTABLE"

# 检查编译是否成功
if [ $? -ne 0 ]; then
    echo "Build failed"
    exit 1
fi

# 拷贝src目录到输出目录
mkdir -p "$OUTPUT_DIR/$HTML_DIR"
echo "copy $HTML_DIR to $OUTPUT_DIR..."
cp -r "$HTML_DIR" "$OUTPUT_DIR"

echo "Build successful! Output files:"
echo " - Executable: $OUTPUT_DIR/$EXECUTABLE"